// components/calendar/calendar.js
Component({
  lifetimes: {
    created() {
      
    }
  },

  pageLifetimes: {
    show(){
      this._render();
    }
  },

  /**
   * 组件的属性列表
   */
  properties: {
  },

  /**
   * 组件的初始数据
   */
  data: {
    week: ['日', '一', '二', '三', '四', '五', '六'],
    date: [],
    activeMonthIndex: -1,
    year: "",
    month: ""
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _getMonth(year, month) {
      if (!year && !month) {
        let date = new Date();
        year = date.getFullYear(),
        month = date.getMonth() + 1;
      }

      let months = new Date(year, month, 0);
      return months.getDate();
    },

    _render(){
      let _date = new Date(),
        year = _date.getFullYear(),
        month = _date.getMonth() + 1,
        day = new Date(Date.parse(year + '/' + month + '/1')).getDay();

      let date = new Array(day);

      for (let i = 0; i < this._getMonth(year, month); i++) {
        date.push({
          text: i + 1
        });
      }
      this.setData({ 
        date,
        year,
        month
      });
    },

    _getMonthItem(e){
      const td = this.data, ed = e.currentTarget.dataset;
      
      this.setData({
        activeMonthIndex: td.activeMonthIndex == ed.index ? -1 : ed.index
      });
    }
  }
})
